﻿using UnityEngine;
using System.Collections;

namespace TowerDefence
{
    public class CameraController : MonoBehaviour
    {
        public Transform[] camAngles; // 摄像机旋转的目标位置和旋转角度数组

        private KeyCode[] clockwiseMovement = new KeyCode[2]; // 顺时针旋转的按键数组
        private KeyCode[] counterClockwiseMovement = new KeyCode[2]; // 逆时针旋转的按键数组

        int index; // 当前目标位置和旋转角度数组的索引
        bool canRotate; // 是否可以旋转

        public void Start()
        {
            // 初始化索引和旋转状态
            index = 0;
            canRotate = true;

            // 初始化摄像机位置和旋转角度
            transform.position = camAngles[0].position;
            transform.rotation = camAngles[0].rotation;

            // 初始化顺时针和逆时针旋转的按键
            clockwiseMovement[0] = KeyCode.RightArrow;
            clockwiseMovement[1] = KeyCode.D;
            counterClockwiseMovement[0] = KeyCode.LeftArrow;
            counterClockwiseMovement[1] = KeyCode.A;
        }

        public void LateUpdate()
        {
            // 如果按下顺时针旋转的按键且可以旋转，或者按下逆时针旋转的按键且可以旋转
            if (Input.GetKey(clockwiseMovement[0]) && canRotate
                || Input.GetKey(clockwiseMovement[1]) && canRotate)
            {
                RightCamViewButton(); // 执行顺时针旋转操作
                canRotate = false; // 禁止连续旋转
            }
            // 如果按下逆时针旋转的按键且可以旋转，或者按下逆时针旋转的按键且可以旋转
            else if (Input.GetKey(counterClockwiseMovement[0]) && canRotate
                || Input.GetKey(counterClockwiseMovement[1]) && canRotate)
            {
                LeftCamViewButton(); // 执行逆时针旋转操作
                canRotate = false; // 禁止连续旋转
            }
            // 如果释放顺时针或逆时针旋转的按键
            else if (Input.GetKeyUp(clockwiseMovement[0]) || Input.GetKeyUp(counterClockwiseMovement[0])
                || Input.GetKeyUp(clockwiseMovement[1]) || Input.GetKeyUp(counterClockwiseMovement[1]))
            {
                canRotate = true; // 允许下一次旋转
            }
        }

        public void RightCamViewButton()
        {
            index++; // 切换到下一个目标位置和旋转角度

            // 循环索引，确保不越界
            if (index > camAngles.Length - 1)
                index = 0;

            // 更新摄像机位置和旋转角度
            transform.position = camAngles[index].position;
            transform.rotation = camAngles[index].rotation;
        }

        public void LeftCamViewButton()
        {
            index--; // 切换到上一个目标位置和旋转角度

            // 循环索引，确保不越界
            if (index < 0)
                index = camAngles.Length - 1;

            // 更新摄像机位置和旋转角度
            transform.position = camAngles[index].position;
            transform.rotation = camAngles[index].rotation;
        }

    }
}


//GameManager gameManager;
//public LayerMask ground;
//public LayerMask wall;

//public float speed = 5;
//public float wallDistance = 10;

//Vector3 moveDir;
//Vector3 curVel;
//float lastFrameTime;

//if (gameManager.onTwoDMode) return;

//float myDeltaTime = Time.realtimeSinceStartup - lastFrameTime;
//lastFrameTime = Time.realtimeSinceStartup;

//float x = Input.GetAxisRaw(StringData.horizontal);
//float z = Input.GetAxisRaw(StringData.vertical);

//Vector3 origin = transform.position;
//RaycastHit hit;

//// camera movement direction
//moveDir = new Vector3(x, 0, z);
//moveDir = moveDir.normalized;

//if (Physics.Raycast(origin, moveDir * .1f, out hit, wall))
//{
//    if (hit.collider.tag.Equals(StringData.wallTag) && hit.distance <= wallDistance)
//        return;
//}

//// allow camera movement
//transform.Translate(moveDir * speed * myDeltaTime);